package com.example.poverty.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.poverty.entity.VisitorWork;
import com.example.poverty.service.VisitorWorkService;
import com.example.poverty.utils.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 前端 走访户工作 控制类
 */
@RestController
@RequestMapping("/visitorWork")
public class VisitorWorkController {

    @Autowired
    private VisitorWorkService visitorWorkService;

    /**
     * 添加走访户工作信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Object addVisitorWork(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String name = request.getParameter("name").trim();  //村户户主
        String dateVisit = request.getParameter("dateVisit").trim();  //走访日期
        String familySituation = request.getParameter("familySituation").trim();  //家庭情况
        String familySituationIllustrate = request.getParameter("familySituationIllustrate").trim();  //家庭情况说明
        String popularSentiment = request.getParameter("popularSentiment").trim();  //民情心声
        String implementationStatus = request.getParameter("implementationStatus").trim();  //民情落实
        String employmentChildren = request.getParameter("employmentChildren").trim();  //子女就业
        String remark = request.getParameter("remark").trim();  //备注

        // 把走访日期转成Date格式
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date timeDate = new Date();
        try {
            timeDate = dateFormat.parse(dateVisit);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        VisitorWork visitorWork = new VisitorWork();
        visitorWork.setName(name);
        visitorWork.setDateVisit(timeDate);
        visitorWork.setFamilySituation(familySituation);
        visitorWork.setFamilySituationIllustrate(familySituationIllustrate);
        visitorWork.setPopularSentiment(popularSentiment);
        visitorWork.setImplementationStatus(implementationStatus);
        visitorWork.setEmploymentChildren(employmentChildren);
        visitorWork.setRemark(remark);
        boolean flag = visitorWorkService.insert(visitorWork);
        if (flag) {
            jsonObject.put(Const.CODE, 1);
            jsonObject.put(Const.MSG, "添加成功");
            return jsonObject;
        }
        jsonObject.put(Const.CODE, 0);
        jsonObject.put(Const.MSG, "添加失败");
        return jsonObject;
    }

    /**
     * 更新走访户工作信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object updateVisitorWork(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String id = request.getParameter("id").trim();  //主键
        String name = request.getParameter("name").trim();  //村户户主
        String dateVisit = request.getParameter("dateVisit").trim();  //走访日期
        String familySituation = request.getParameter("familySituation").trim();  //家庭情况
        String familySituationIllustrate = request.getParameter("familySituationIllustrate").trim();  //家庭情况说明
        String popularSentiment = request.getParameter("popularSentiment").trim();  //民情心声
        String implementationStatus = request.getParameter("implementationStatus").trim();  //民情落实
        String employmentChildren = request.getParameter("employmentChildren").trim();  //子女就业
        String remark = request.getParameter("remark").trim();  //备注

        // 把走访日期转成Date格式
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date timeDate = new Date();
        try {
            timeDate = dateFormat.parse(dateVisit);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        VisitorWork visitorWork = new VisitorWork();
        visitorWork.setId(Integer.parseInt(id));
        visitorWork.setName(name);
        visitorWork.setDateVisit(timeDate);
        visitorWork.setFamilySituation(familySituation);
        visitorWork.setFamilySituationIllustrate(familySituationIllustrate);
        visitorWork.setPopularSentiment(popularSentiment);
        visitorWork.setImplementationStatus(implementationStatus);
        visitorWork.setEmploymentChildren(employmentChildren);
        visitorWork.setRemark(remark);
        boolean flag = visitorWorkService.update(visitorWork);
        if (flag) {
            jsonObject.put(Const.CODE, 1);
            jsonObject.put(Const.MSG, "修改成功");
            return jsonObject;
        }
        jsonObject.put(Const.CODE, 0);
        jsonObject.put(Const.MSG, "修改失败");
        return jsonObject;
    }

    /**
     * 删除走访户工作信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Object deleteVisitorWork(HttpServletRequest request) {
        String id = request.getParameter("id").trim();  //主键
        boolean flag = visitorWorkService.delete(Integer.parseInt(id));
        return flag;
    }

    /**
     * 根据主键查询走访户工作信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/selectByPrimaryKey", method = RequestMethod.GET)
    public Object selectByPrimaryKey(HttpServletRequest request) {
        String id = request.getParameter("id").trim();  //主键
        return visitorWorkService.selectByPrimaryKey(Integer.parseInt(id));
    }

    /**
     * 查询所有的走访户工作信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/allVisitorWork", method = RequestMethod.GET)
    public Object allVisitorWork(HttpServletRequest request) {
        return visitorWorkService.allVisitorWork();
    }

    /**
     * 根据村户户主查询走访户工作信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getByName", method = RequestMethod.GET)
    public Object getByName(HttpServletRequest request) {
        String name = request.getParameter("name").trim();  //村户户主
        return visitorWorkService.getByName(name);
    }

}
